<template>
  <div>
    <!-- 订货单审核-->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110" @resetChange="resetChange"></TopFilter>
    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        columnsRef="zjOrderDetailQuerys"
        :isSelectColumn="false"
        :onRowSelectChange="selectChangeHandle"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls" slot-scope="props">
          <multiuse-button size="small" type="primary" @click="addHandle()">导 出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>
    <BaseDialog :visible.sync="selectDelearFlag" :title="'经销商选择'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <select-delear-page @close="Delearclose" @save="Delearsave"></select-delear-page>
    </BaseDialog>
    <Drawer :visible.sync="detailVisible" :title="'订单信息'" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <detailModel :rowData="formData" @close="emitcloseHandler" />
    </Drawer>
  </div>
</template>
<script>
import { getList, getNotPage, vehicleConfiguration, exporturl } from '@/api/omSaleP/KeyCustomers/OrderQuery/index.js';
import detailModel from './detailModel';
import { dictionary } from '@/utils/dictMixin';
import selectDelearPage from './selectDelearPage.vue';
export default {
  name: 'acknowledgementOrder',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: {
        searchType: 4,
        majorType: 3
      },
      selectDelearFlag: false,
      fetchApi: getList,
      formData: {},
      exportLoading: false,
      issearch: false,
      detailVisible: false,
      ProjectId: ''
    };
  },
  components: {
    detailModel,
    selectDelearPage
  },
  mixins: [dictionary],
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getvehicleList();
    this.gettmFiAccountType();
    this.getcarList();
  },
  methods: {
    createdFrom() {
      return [
         {
          type: 'SELECT',
          label: '订单类型',
          fieldName: 'orderType',
          placeholder: '请选择',
          itemList: this.createDictList('2106'),
          change: p => {
            if (p) {
              this.topFilterList.find(x => x.fieldName == 'orderDetailType').itemList = [];
              this.topFilterList.find(x => x.fieldName == 'orderDetailType').initialValue = '';
              this.getNotPage(p);
            } else {
              this.topFilterList.find(x => x.fieldName == 'orderDetailType').itemList = [];
              this.topFilterList.find(x => x.fieldName == 'orderDetailType').initialValue = '';
            }
          }
        },
        {
          type: 'INPUT',
          label: '订单编号',
          fieldName: 'doNo',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '车辆用途',
          fieldName: 'orderStatus',
          placeholder: '请选择',
          itemList: this.createDictList('2102')
        },
       
        {
          type: 'INPUT',
          label: '物料编码',
          fieldName: 'doNo',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '物料描述',
          fieldName: 'productName',
          placeholder: '请选择',
        },
         {
          type: 'SELECT',
          label: '订单明细状态',
          fieldName: 'orderDetailType',
          placeholder: '请选择',
          itemList: this.createDictList('2109'),
        },
       {
          type: 'INPUT_CASCADER',
          label: '车型选择',
          fieldName: 'vehicleInfo',
          placeholder: '请选择',
          options: { titles: ['车系', '车型', '配置', '选装', '颜色'] },
          itemList: []
        },
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 2)}>
                  详情
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6003'),
          render: p => {
            return <span>{this.createDictText(p.row.dealerCode, '6003')}</span>;
          }
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '大客户来源',
          dataIndex: 'targetSource',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '大客户编号',
          dataIndex: 'bigCustomerId',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '订单编号',
          dataIndex: 'orderId',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
        {
          title: '结算订单编号',
          dataIndex: 'doNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
        {
          title: '订单年月',
          dataIndex: 'orderYearMonth',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '紧急度',
          dataIndex: 'emergencyType',
          sorter: true,
          filter: true,
          filterType: 'input',
          editRequired: true,
          rules: [{ required: true, message: '请填写', trigger: 'blur' }],
          render: p => {
            return (
              <el-select
                size="mini"
                v-model={p.row.emergencyType}
                placeholder="请选择"
                onChange={value => {
                  p.row.emergencyType = value;
                }}
              >
                {this.createDictList('2101').map(item => (
                  <el-option key={item.value} label={item.text} value={item.value} />
                ))}
              </el-select>
            );
          }
        },
        {
          title: '订单类型',
          dataIndex: 'orderType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('2106'),
          render: p => {
            return <span>{this.createDictText(p.row.orderType, '2106')}</span>;
          }
        },
        {
          title: '订单细分类型',
          dataIndex: 'orderDetailType',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车辆用途',
          dataIndex: 'vehicleUse',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: p => {
            return <span>{this.createDictText(p.row.vehicleUse, '2406')}</span>;
          }
        },
{
          title: '订单明细状态',
          dataIndex: 'orderDetailStatus',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '资金类型',
          dataIndex: 'accountTypeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '市场指导价',
          dataIndex: 'singleAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '采购价格',
          dataIndex: 'purchaseMoney',
          sorter: true,
          filter: true,
          precision: 2,
          filterType: 'input'
        },
        {
          title: '现金折让',
          dataIndex: 'discountAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '建店返利',
          dataIndex: 'buildRebateAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '是否使用返利',
          dataIndex: 'rebateAmount',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: p => {
            return <span>{this.createDictText(p.row.rebateAmount, '1004')}</span>;
          }
        },
        {
          title: '返利池返利',
          dataIndex: 'singleRebateAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '运输方式',
          dataIndex: 'transportType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6004'),
          render: p => {
            return <span>{this.createDictText(p.row.transportType, '6004')}</span>;
          }
        },
        {
          title: '收车地址',
          dataIndex: 'address',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '送达城市',
          dataIndex: 'powerCompany',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '联系人',
          dataIndex: 'linkMan',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '联系电话',
          dataIndex: 'linkPhone',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '物料编码',
          dataIndex: 'materialCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '物料描述',
          dataIndex: 'materialDescription',
          align: 'center',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车系',
          dataIndex: 'seriesName',
          width: 120,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型',
          dataIndex: 'modelName',
          width: 120,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配置',
          dataIndex: 'packageName',
          width: 120,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选装',
          dataIndex: 'optionName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '内饰',
          dataIndex: 'trimName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '外饰',
          dataIndex: 'colorName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '提报时间',
          dataIndex: 'reportDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '业务审核时间',
          dataIndex: 'checkDate',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '财务审核时间',
          dataIndex: 'financeCheckDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '出库时间',
          dataIndex: 'sapOutDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '发运时间',
          dataIndex: 'commandDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '到店交付时间',
          dataIndex: 'requiredDeliveryDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        }
      ];
    },
    // 经销商代码选择
    SelectDelear() {
      this.selectDelearFlag = true;
    }, //关闭
    Delearclose() {
      this.selectDelearFlag = false;
    },
    Delearsave(obj) {
      this.topFilterInit.map(x => {
        if ('ownerCode' == x.fieldName) {
          x.initialValue = obj[0].dealerCode; //维修工单号返显
        }
      });
      this.selectDelearFlag = false;
    },
    selectChangeHandle(row) {},
    // 获取车型车系
    async getvehicleList() {
      let res = await vehicleConfiguration(0, { isUnlimited: 10041001 });
      this.topFilterList.find(item => item.fieldName === 'brandProductList').itemList = res.data;
    },
     async getcarList() {
      let res = await vehicleConfiguration(0, { isUnlimited: 10041001 });
      console.info('res:',res)
      this.topFilterList.find(item => item.fieldName === 'vehicleInfo').itemList = res.data;
    },
    async getDeptList() {
      let res = await getDeptList({ orgType: 15061005 });
      let arr = res.data.map(x => ({ text: x.orgName, value: x.orgId }));
      this.topFilterList.find(x => x.fieldName == 'deptId').itemList = arr;
    },
    async getOfficeList(orgid) {
      let res = await getOfficeList({ orgType: 15061007, parentOrgId: orgid });
      let arr = res.data.map(x => ({ text: x.orgName, value: x.orgId }));
      this.topFilterList.find(x => x.fieldName == 'officeId').itemList = arr;
      this.topFilterList.find(x => x.fieldName == 'officeId').initialValue = '';
    },
    async getNotPage(orderType) {
      let list = await getNotPage({ orderType: orderType });
      this.topFilterList.map(x => {
        if (x.fieldName == 'orderDetailType') {
          x.itemList = list.data.map(x => ({ text: x.subName, value: x.subId }));
        }
      });
    },
    async gettmFiAccountType() {
      let res = (await gettmFiAccountType({ fundsType: 0 })).data.map(x => ({ text: x.accountTypeName, value: x.id }));
      this.topFilterList.find(x => x.fieldName == 'accountType').itemList = res;
    },
    async addHandle() {
      let json = Object.assign({}, { CODE: 'CODE_0000011' }, this.fetchParams);
      let res = await exporturl(json);

      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
      //   confirmButtonText: '下载',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     let json = this.fetchParams;
      //     this.exportLoading = true;

      //     exportList(json)
      //       .then(response => {
      //         console.log(response, 'response');
      //         exportFile(response.data, '订单明细查询', 'csv');
      //         this.exportLoading = false;
      //       })
      //       .catch(err => {
      //         console.log(err);
      //       });
      //   })
      //   .catch(() => {
      //     console.log('456');
      //     this.$message({
      //       type: 'info',
      //       message: '已取消'
      //     });
      //   });
    },
    changeHandle(params) {
      this.fetchParams.projectCode = this.ProjectId;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    editHandle(data) {
      this.formData = data;
      this.detailVisible = true;
    },
    emitcloseHandler() {
      this.detailVisible = false;
    },
    dealercloseHandler(val) {
      if (val) {
        this.topFilterList.find(x => x.fieldName == 'dealerName').initialValue = val.dealerName;
        this.dealerCode = val.dealerCode;
      }
      this.issearch = false;
    },
    resetChange(val) {
      this.ProjectId = '';
      this.fetchParams.projectCode = '';
      this.fetchParams = Object.assign({}, this.fetchParams);
    }
  }
};
</script>
